package com.cjg.juc;

public class pc {
    public static void main(String[] args) {
        A a = new A();
        new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    a.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();   new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    a.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();
        new Thread(()->{
            for (int i = 0; i <5 ; i++) {
                try {
                    a.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
class A{
    private int unm=0;
    public synchronized void increment() throws InterruptedException {
        while (unm!=0){
            this.wait();
        }
        unm++;
        System.out.println(Thread.currentThread().getName()+":"+unm);
        this.notifyAll();
    }
 public synchronized void decrement() throws InterruptedException {
        while (unm==0){
            this.wait();
        }
        unm--;
     System.out.println(Thread.currentThread().getName()+":"+unm);
        this.notifyAll();
    }
}
